package com.ruicar.afs.cloud.parameter.commom.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.parameter.commom.entity.TsysVehicleDetail;
import com.ruicar.afs.cloud.parameter.commom.entity.TsysVehicleModel;
import com.ruicar.afs.cloud.parameter.commom.entity.VeBrand;
import com.ruicar.afs.cloud.parameter.commom.entity.VeMain;
import com.ruicar.afs.cloud.parameter.commom.mapper.TsysVehicleModelMapper;
import com.ruicar.afs.cloud.parameter.commom.service.TsysVehicleDetailService;
import com.ruicar.afs.cloud.parameter.commom.service.TsysVehicleModelService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @description: 车型表
 * @author: sijun.yu
 * @date: 2020/5/15 9:45
 */
@Slf4j
@Service
@AllArgsConstructor
public class TsysVehicleModelServiceImpl extends ServiceImpl<TsysVehicleModelMapper, TsysVehicleModel> implements TsysVehicleModelService {
    private final TsysVehicleDetailService tsysVehicleDetailService;

    @Override
    public void synchroVehicle(List<VeBrand> brands, List<VeMain> mains) throws ParseException {
        DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(brands.size()>0){
            for (VeBrand brand:brands){
                TsysVehicleModel model = this.baseMapper.selectOne(Wrappers.<TsysVehicleModel>query().lambda()
                        .eq(TsysVehicleModel::getCode,brand.getCode()));
                if(model==null){
                    // 说明没有 我要去添加
                    TsysVehicleModel m=new TsysVehicleModel();
                    m.setCode(brand.getCode());
                    m.setModels(brand.getBrand());
                    m.setBrandCode(brand.getMaker());
                    m.setOrderId(Integer.valueOf(brand.getOrderid()));
                    m.setIsActive(brand.getIsactive());
                    m.setStyleRule(brand.getStylerule());
                    m.setFullName(brand.getFullname());
                    m.setCmsId(brand.getCmsid());
                    m.setType(brand.getType());
                    m.setSeriesId(brand.getSeriesid());
                    m.setAdditionalTag(brand.getAdditionaltag());
                    m.setNeedSync(brand.getNeedsync());
                    m.setCreateTime(fmt.parse(brand.getCdate()));
                    m.setCreateBy(brand.getCreateuser());
                    m.setUpdateTime(fmt.parse(brand.getUpdatedate()));
                    m.setUpdateBy(brand.getUpdateuser());
                    m.setDelFlag(brand.getIsdelete());
                    this.baseMapper.insert(m);
                }else {
                    // 说明存在去更新
                    model.setCode(brand.getCode());
                    model.setModels(brand.getModels());
                    model.setBrandCode(brand.getMaker());
                    model.setOrderId(Integer.valueOf(brand.getOrderid()));
                    model.setIsActive(brand.getIsactive());
                    model.setStyleRule(brand.getStylerule());
                    model.setFullName(brand.getFullname());
                    model.setCmsId(brand.getCmsid());
                    model.setType(brand.getType());
                    model.setSeriesId(brand.getSeriesid());
                    model.setAdditionalTag(brand.getAdditionaltag());
                    model.setNeedSync(brand.getNeedsync());
                    model.setCreateTime(fmt.parse(brand.getCdate()));
                    model.setCreateBy(brand.getCreateuser());
                    model.setUpdateTime(fmt.parse(brand.getUpdatedate()));
                    model.setUpdateBy(brand.getUpdateuser());
                    model.setDelFlag(brand.getIsdelete());
                    this.baseMapper.updateById(model);
                }
            }
        }
        if(mains.size()>0){
            for (VeMain main:mains){
                TsysVehicleDetail detail =tsysVehicleDetailService.getOne(Wrappers.<TsysVehicleDetail>query().lambda()
                        .eq(TsysVehicleDetail::getCode,main.getCode()).eq(TsysVehicleDetail::getModelCode,main.getBrandcode()));
                if(detail==null){
                    TsysVehicleDetail t =new TsysVehicleDetail();
                    t.setModelCode(main.getBrandcode());
                    t.setCode(main.getCode());
                    t.setName(main.getName());
                    t.setGuidePrice(new BigDecimal(main.getGuideprice()));
                    t.setIsActive(main.getIsactive());
                    t.setOrderId(Integer.valueOf(main.getOrderid()));
                    t.setDisplacement(main.getVehiclemain05());
                    t.setTransmission(main.getVehiclemain06());
                    t.setConfigure(main.getVehiclemain07());
                    t.setYearStyle(main.getYearstyle());
                    t.setAssetsCode(main.getZichancode());
                    t.setPowerType(main.getDonglitype());
                    t.setDriver(main.getDriver());
                    t.setBlowoff(main.getPaifang());
                    t.setCmsId(main.getCmsid());
                    t.setCreateTime(fmt.parse(main.getCdate()));
                    t.setRedBlue(main.getVemain01());
                    t.setMemo(main.getMemo());
                    t.setInterior(main.getVemain02());
                    t.setModelId(main.getModelid());
                    t.setChe300Style(main.getChe300style());
                    t.setCarLength(new BigDecimal(main.getChelong()));
                    t.setWight(new BigDecimal(main.getWight()));
                    t.setSeatNum(new BigDecimal(main.getSeatnum()));
                    t.setCarLevel(main.getCarlevel());
                    t.setIsParallel(main.getIsparallel());
                    t.setNeedSync(main.getNeedsync());
                    t.setIsInternet(main.getIschelianwag());
                    t.setIsGreen(main.getIsgreen());
                    t.setCarType(main.getCartype());
                    t.setCarTypeDetail(main.getCartypedetail());
                    t.setUpdateTime(fmt.parse(main.getUpdatedate()));
                    t.setUpdateBy(main.getUpdateuser());
                    t.setCreateBy(main.getCreateuser());
                    tsysVehicleDetailService.save(t);

                }else {
                    detail.setModelCode(main.getBrandcode());
                    detail.setCode(main.getCode());
                    detail.setName(main.getName());
                    detail.setGuidePrice(new BigDecimal(main.getGuideprice()));
                    detail.setIsActive(main.getIsactive());
                    detail.setOrderId(Integer.valueOf(main.getOrderid()));
                    detail.setDisplacement(main.getVehiclemain05());
                    detail.setTransmission(main.getVehiclemain06());
                    detail.setConfigure(main.getVehiclemain07());
                    detail.setYearStyle(main.getYearstyle());
                    detail.setAssetsCode(main.getZichancode());
                    detail.setPowerType(main.getDonglitype());
                    detail.setDriver(main.getDriver());
                    detail.setBlowoff(main.getPaifang());
                    detail.setCmsId(main.getCmsid());
                    detail.setCreateTime(fmt.parse(main.getCdate()));
                    detail.setRedBlue(main.getVemain01());
                    detail.setMemo(main.getMemo());
                    detail.setInterior(main.getVemain02());
                    detail.setModelId(main.getModelid());
                    detail.setChe300Style(main.getChe300style());
                    detail.setCarLength(new BigDecimal(main.getChelong()));
                    detail.setWight(new BigDecimal(main.getWight()));
                    detail.setSeatNum(new BigDecimal(main.getSeatnum()));
                    detail.setCarLevel(main.getCarlevel());
                    detail.setIsParallel(main.getIsparallel());
                    detail.setNeedSync(main.getNeedsync());
                    detail.setIsInternet(main.getIschelianwag());
                    detail.setIsGreen(main.getIsgreen());
                    detail.setCarType(main.getCartype());
                    detail.setCarTypeDetail(main.getCartypedetail());
                    detail.setUpdateTime(fmt.parse(main.getUpdatedate()));
                    detail.setUpdateBy(main.getUpdateuser());
                    detail.setCreateBy(main.getCreateuser());
                    tsysVehicleDetailService.updateById(detail);
                }
            }
        }
    }
}
